<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>拓展学习-数组的reduce方法</title>
    </head>
    <body>

    </body>
    <script>
        const arr = [
            {id: 1, name: '西瓜', state: true, price: 10, count: 1},
            {id: 2, name: '哈密瓜', state: true, price: 11, count: 3},
            {id: 3, name: '桃子', state: true, price: 12, count: 5},
            {id: 4, name: '苹果', state: false, price: 13, count: 11},
        ]
        //把购物车中已经勾选的结果总结算出来
        let sumPrice = 0;
        arr.filter(item => item.state).forEach(item => {
            sumPrice += item.price * item.count;
        });
        console.log('已勾选的总价为', sumPrice);
        console.log('*********reduce方法*********');
        //后面0是初始值，语法为：arr.filter(item=>item.state).reduce((累加结果,当前项)=>{},0);
        const  sumPrice2 = arr.filter(item => item.state).reduce((sum, item) => {
            return sum + item.price * item.count;
        }, 0);
        console.log('已勾选的总价为', sumPrice2);
    </script>
</html>